var resdata;
var url;
var trStr;
var Data ={"Goods":[]};
var sendData = [];
$(document).ready(function(){
	StoreName = decodeURI(decodeURI(StoreName))
	Statue = decodeURI(decodeURI(Statue))
	$('#nameh').append(StoreNO+"."+StoreName);
	$('#Statueh').append("进行中");
	$('#name').text(sessionStorage.getItem("name"));
	if(nw=="T"){
		$.ajax({
				url: "../../typd/InventoryEdit"
				,type:'post'
				,data:JSON.stringify({"PaperNO":BillNO})
				,contentType: "application/json; charset=utf-8"
				,dataType:'json'
				,success: function(res){
							for (var i = 0; i < res.data.length; i++) {
								if(res.data[i].CheckQty == 0){res.data[i].CheckQty ='';}
							trStr   +='<tr>'
									+'<td>'+res.data[i].GoodsNO+'</td>'
									+'<td>'+res.data[i].GoodsName+'</td>'
									+'<td>'+res.data[i].Baseunit+'</td>'
									+'<td id="CheckQty'+i+'" contentEditable="true" >'+res.data[i].CheckQty+'</td>'
									+'</tr>'
							}
					$('#shujvdiv').append(trStr);
							for(var i = 0; i < res.data.length; i++){
								
										
									$("#CheckQty"+i).bind("keydown",function(e){
										//console.log(e)
									　　// 兼容FF和IE和Opera
									　　var theEvent = e || window.event;
									　　var code = theEvent.keyCode || theEvent.which || theEvent.charCode;
									　　 if (code == 13) {
									　　//回车执行查询
									　     	
										
											var ii = Number(theEvent.target.id.substring(8,11));
											var ss = ii+1;
											
											//$("#"+ss).trigger("click");
											if(document.getElementById('CheckQty'+ss)!= null){
												document.getElementById('CheckQty'+ss).focus();
											}else{
												document.getElementById('CheckQty0').focus();
											}
											
									　　}
									});
									
							
					
								
							document.getElementById('CheckQty'+i).oninput = function () {
						      this.innerHTML = this.innerHTML.replace(/[\D]/g, '')
						      keepLastIndex(this)
						   }
			   			  }
				}
			});
	}else{
			$.ajax({
				url: "../../typd/InventoryEdit"
				,type:'post'
				,data:JSON.stringify({"PaperNO":BillNO})
				,contentType: "application/json; charset=utf-8"
				,dataType:'json'
				,success: function(res){
							for (var i = 0; i < res.data.length; i++) {
								if(res.data[i].CheckQty == 0){res.data[i].CheckQty ='';}
							trStr   +='<tr>'
									+'<td>'+res.data[i].GoodsNO+'</td>'
									+'<td>'+res.data[i].GoodsName+'</td>'
									+'<td>'+res.data[i].Baseunit+'</td>'
									+'<td id="CheckQty'+i+'" contentEditable="true" >'+res.data[i].CheckQty+'</td>'
									+'</tr>'
							}
					$('#shujvdiv').append(trStr);
							for(var i = 0; i < res.data.length; i++){
								
							
									$("#CheckQty"+i).bind("keydown",function(e){
										//console.log(e)
									　　// 兼容FF和IE和Opera
									　　var theEvent = e || window.event;
									　　var code = theEvent.keyCode || theEvent.which || theEvent.charCode;
									　　 if (code == 13) {
									　　//回车执行查询
									　     	
										
											var ii = Number(theEvent.target.id.substring(8,11));
											var ss = ii+1;
											if(document.getElementById('CheckQty'+ss)!= null){
												document.getElementById('CheckQty'+ss).focus();
											} 
									　　}
									});
									
						
								
							document.getElementById('CheckQty'+i).oninput = function () {
						      this.innerHTML = this.innerHTML.replace(/[\D]/g, '')
						      keepLastIndex(this)
						   }
			   			  }
	
	
					
				}
			});
	}
});
function save() {
	sendData = [];
	Data ={};
    var tableObj = document.getElementById('oTable');
    for (var i = 1; i < tableObj.rows.length; i++) {    //遍历Table的所有Row
			var send = {};
			send.PaperNO =BillNO;
			send.StoreNO =StoreNO;
			send.GoodsNO = tableObj.rows[i].cells[0].innerText;
			if(tableObj.rows[i].cells[3].innerText==""){
				send.CheckQty= '0' ;
			}else{
				send.CheckQty=tableObj.rows[i].cells[3].innerText;
			}
			sendData.push(send);
    }
	Data.Goods=sendData;
	$.ajax({
		url: "../../typd/pdsave"
		,type:'post'
		,data:JSON.stringify(Data)
		,contentType: "application/json; charset=utf-8"
		,dataType:'json'
		,success: function(res){
			layer.msg('保存成功',{time:500},function(){
							dd.device.base.getPhoneInfo({
							    onSuccess : function(data) {//iPhone
				                    if(data.brand == iPhone){
										sessionStorage.setItem("Creload", true);
									}
							    },
							    onFail : function(err) {}
							});
							goback();
						});
		
		}
	});
}
function goback(){
		if(nw=="T"){
	history.go(-2);
	}else{
			history.go(-1);
	}
}
function submitFun(){
	
}
function keepLastIndex(obj) {
      if (window.getSelection) {
        obj.focus();
        var range = window.getSelection();
        range.selectAllChildren(obj);
        range.collapseToEnd();
      }
      else if (document.selection) {
        var range = document.selection.createRange();
        range.moveToElementText(obj);
        range.collapse(false);
        range.select();
      }
    }